<?php
/**
 * 订单管理
 * edition ：v1
 * create time：2021-06-08
 * author: acker
 */
namespace App\Admin\Controllers\Order;


use App\Admin\Actions\NewworkOrder\OrderService;
use App\Admin\Exporter\Order\OrderExpoter;
use App\Http\Controllers\Controller;
use App\Models\DataDictionaryModel;
use App\Models\yb\GoodsModel;
use App\Models\yb\GoodsOrderDetailModel;
use App\Models\yb\GoodsOrderModel;
use App\Models\yb\GoodsTypeModel;
use App\Models\yb\UserAddressModel;
use App\Models\yb\UserModel;
use Encore\Admin\Form;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use Encore\Admin\Widgets\Table;



class OrderController extends Controller
{
    use ModelForm;

    protected $header = '订单管理';

    /**
     * 工作状态列表
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header($this->header);
            $content->description('列表');
            $content->body($this->grid());

        });
    }

    /**
     * 新增
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header($this->header);
            $content->description('添加');
            $content->body($this->form());

        });
    }

    /**
     * 编辑
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header($this->header);
            $content->description('编辑');
            $content->body($this->form()->edit($id));

        });
    }

    /**
     * 编辑
     * @return Content
     */
    public function show($id)
    {
        return Admin::content(function (Content $content) use ($id) {
            $order_data = GoodsOrderModel::where(['id'=>$id])->first();
            if($order_data['status'] == 0){
                $order_data['status'] = "待支付";
            }elseif($order_data['status'] == 1){
                $order_data['status'] = "待接单";
            }elseif($order_data['status'] == 2){
                $order_data['status'] = "待配送";
            }elseif($order_data['status'] == 3){
                $order_data['status'] = "已送达";
            }elseif($order_data['status'] == 4){
                $order_data['status'] = "已完成";
            }
            if($order_data['type'] == 1){
                $order_data['type'] = "水票支付";
            }elseif($order_data['status'] == 2){
                $order_data['type'] = "微信支付";
            }elseif($order_data['status'] == 3){
                $order_data['type'] = "混合支付";
            }
            $user_data = UserModel::where(['id'=>$order_data['user_id']])->first();
          
            $user_address_data = UserAddressModel::where(['id'=>$order_data['address_id']])->first();

            $order_detail_data = GoodsOrderDetailModel::where(['order_sn'=>$order_data['order_sn']])->get();

            foreach($order_detail_data as $key=>$val){
                $goods_data = GoodsModel::where(['id'=>$val['goods_id']])->first();
                $goods_data['goods_cover'] = env('OSS_URL').$goods_data['goods_cover'];
                $goods_data['goods_type_name'] = GoodsTypeModel::selectValue('id',$goods_data['goods_type']);
                $order_detail_data[$key]['goods_data'] = $goods_data;
            }

            $content->header($this->header);
            $content->description('详情');
            $content->body(view('admin.order.detail',[
                'user_data' => $user_data,
                'order_data' => $order_data,
                'user_address_data' => $user_address_data,
                'order_detail_data' => $order_detail_data
            ]));

        });
    }

    public function grid(){

        $grid = new Grid(new GoodsOrderModel());

        if(Admin::user()->id ==1){
            $grid->model()->orderBy('created_at', 'DESC');
        }else{
            $grid->model()->where(['network_id'=>admin::user()->id])->orderBy('created_at', 'DESC');
        }

        $grid->column('order_sn','订单编号')->display(function($filed){
            return empty($filed) ? config('view.EmptyData') : $filed;
        });

        $grid->column('User.nickname','下单人昵称')->display(function($filed){
            return empty($filed) ? config('view.EmptyData') : $filed;
        });

        $grid->column('User.phone','下单手机号')->display(function($filed){
            return empty($filed) ? config('view.EmptyData') : $filed;
        });

        $grid->column('Network.name','网点名称')->display(function($filed){
            return empty($filed) ? config('view.EmptyData') : $filed;
        });

        $grid->column('total_price','订单金额')->display(function($filed){
            return empty($filed) ? config('view.EmptyData') : "¥".$filed;
        });

        $grid->column('amount','实付金额')->display(function($filed){
            return empty($filed) ? config('view.EmptyData') : "¥".$filed;
        });

//        $grid->column('Goods.goods_title', '商品详情')->display(function($filed){
//            return empty($filed) ? '暂无' : $filed;
//        })->modal('商品详情', function($model){
//            $goods_data = GoodsModel::where(['id'=>$this->goods_id])->select('id','goods_type','goods_title','goods_price','goods_specs','status')->get();
//            $goods_data = $goods_data->toArray();
//            $goods_data[0]['goods_type'] = GoodsTypeModel::selectValue('id',$goods_data[0]['goods_type']);
//            if($goods_data[0]['status']==0){
//                $goods_data[0]['status']="禁用";
//            }else{
//                $goods_data[0]['status']="启用";
//            }
//            unset($goods_data[0]['action_id']);
//            $goods_data[0]['number'] = $this->number;
//            return new Table(['编号','类型','标题','单价','规格','状态','下单数量'], $goods_data);
//        });

        $grid->column('status','订单状态')->using([
            '0'=>'待支付',
            '1'=>'待接单',
            '2'=>'待配送',
            '3'=>'已送达',
            '4'=>'已完成'
        ], '未知')->dot([
            0 => 'danger',
            1 => 'info',
            2 => 'warning',
            3 => 'primary',
            4 => 'success',
        ], 'success');

        $grid->column('created_at','创建时间')->display(function($filed){
            return empty($filed) ? config('view.EmptyData') : $filed;
        })->sortable();

        //搜索
        $grid->filter(function($filter){
            $filter->column(1/2, function ($filter) {
                $filter->like('order_sn','订单编号');
                $filter->like('Order.goods_title','商品名称');
                $filter->like('User.nickname','用户名称');
                $filter->between('created_at', '下单时间')->datetime();
            });
            $filter->column(1/2, function ($filter) {
                $filter->like('Network.name','网点名称');
                $filter->equal('user_id','用户')->select(UserModel::selectOptions());
                $filter->equal('status', '状态')->select([
                    '0'=>'待支付',
                    '1'=>'待接单',
                    '2'=>'待配送',
                    '3'=>'已送达',
                    '4'=>'已完成'
                ]);
            });
            $filter->disableIdFilter();
        });

        $grid->fixColumns(1, -1);

        $grid->exporter(new OrderExpoter());

        $grid->column('action_id', '送达操作')->display(function ($id, $column) use ($grid) {
            if($this->status == 2){
                $action = new OrderService();
                return $action
                    ->asColumn()
                    ->setGrid($grid)
                    ->setColumn($column)
                    ->setRow($this);
            }
        });

        $grid->actions(function ($actions) {
            // 当前行的数据数组
            $actions->disableDelete();
            $actions->disableEdit();
        });

        $grid->disableCreateButton();

        $grid->tools(function ($tools) {
            $tools->batch(function ($batch) {
                $batch->disableDelete();
            });
        });

        $grid->paginate(10);

        return $grid;
    }

    /**
     * @return mixed
     */
    public function form(){

        $form = new Form(new GoodsModel());

        $form->text('goods_title', '商品标题')->rules('required|max:150');

        $form->file('goods_cover','封面图')->uniqueName()->removable();

        $form->select('goods_type', '类型')->options(GoodsTypeModel::selectOptions())->rules('required');

        $form->text('goods_specs', '规格')->rules('required|max:150');

        $form->currency('retail_goods_price', '上货价')->symbol('￥');

        $form->currency('goods_price', '零售价')->symbol('￥');

        $form->number('goods_stock', '库存');

        $form->radio('status', '状态')->options(DataDictionaryModel::GoodsStatus)->default('0');

        $form->editor('goods_detail','详情');

        $form->tools(function (Form\Tools $tools) {
            $tools->disableView();
        });

        $form->footer(function ($footer) {
            // 去掉`查看`checkbox
            $footer->disableViewCheck();
        });

        return $form;
    }



}